import {request} from "./index"
import {UUID} from "../utils/uuid"
// 获取uuid
const getUuid = () => {
 	return	new Promise((resolve,reject)=>{
		 var uuid=wx.getStorageSync('uuid');
		 if(uuid){
			 resolve();
			 return
			}
			uuid=UUID.generate().replace(/-/g,'');
			wx.setStorageSync('uuid',uuid);
			resolve();
		})
}
/** 登录
 * 
 * @param {string} uuid
 * @param {string} encryptedData
 * @param {string} iv
  */
const login = (uuid, encryptedData, iv,code) => {
  return request.get("base/miniProgram/autoLogin", {
    encryptedData,
    uuid: uuid,
    iv,
    code: code,
    type: 'MINI'
  },{
    'uuid': uuid
  })
}
/** 注册openId
 * 
 * @param {string} uuid
 * @param {string} code
  */
const loginByOpenId = (uuid, code) => {
  return request.get("base/miniProgram/register", {
    code,
    type: "MINI"
  }, {
    'uuid': uuid
  })
}
/** 获取用户信息
 * 
 * @param {string} token
  */
const getUserInfo = (token) => {
  return request.get("v1/app/member", {}, {
    'Authorization': token
  })
}
/** 修改用户信息
 * 
 * @param {string} token
 * @param {string} nickName
 * @param {string} face
  */
const editUserInfo = (token, nickName, face, sex) => {
  return request.put("v1/app/member/editOwn", {
    nickName,
    sex,
    face
  }, {
    'Authorization': token,
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
  })
}
export {
  getUuid,
  login,
  loginByOpenId,
  getUserInfo,
  editUserInfo
}